package com.pw.study.structure.stack;


/**
 * @description:简单计算机实现
 * @author: linux_future
 * @date: 2022/11/25
 */
public class Calculator {
    public static void main(String[] args) {
        //1。数据输入
        String line = "17*2*2-15+1-5+3-14";
        FutureArrayStack numStack = new FutureArrayStack(10);
        FutureArrayStack operStack = new FutureArrayStack(10);
        //2.对输入数据解析
        int index = 0;
        int num1 = 0;
        int num2 = 0;

        String keepNum = "";
        char ch = ' ';
        while (true) {
            char n = line.charAt(index);
            index++;
            if (Character.isDigit(n)) {
                keepNum += n + "";
            } else {
                ch = n;
                num2 = Integer.parseInt(keepNum);
                if (numStack.isEmpty()) {
                    numStack.push(num2);
                    operStack.push(ch);
                } else {
                    char peek = (char) operStack.peek();
                    //判断数据
                    if (operStack.priority(ch) <= operStack.priority(peek)) {
                        num1 = numStack.pop();
                        char oper = (char) operStack.pop();
                        int sum = numStack.cal(num1, num2, oper);
                        System.out.println("=====cal:" + "num:" + num1 + " num2:" + num2 + " ch:" + ch + " sum:" + sum);
                        numStack.push(sum);
                        operStack.push(ch);
                    } else {
                        operStack.push(ch);
                    }
                }
                keepNum = "";
            }

            if (index >= line.length()) {
                break;
            }
        }
        num1 = numStack.pop();
        num2 = Integer.parseInt(keepNum);
        char oper = (char) operStack.pop();
        int sum = numStack.cal(num1, num2, oper);
        System.out.println("===" + sum);

    }
}
